//
//  DiaryEditMoodTableViewCell.swift
//  MoodDiary
//
//  Created by MoodDiary on 2020/12/8.
//

import UIKit

protocol DiaryEditMoodTableViewCellDeleagte: class {
    func diaryEditMood(mood: String)
}

@objcMembers
class DiaryEditMoodTableViewCell: UITableViewCell {

    @IBOutlet weak var imgV: UIImageView!
    @IBOutlet weak var collectionView: UICollectionView!
    
    
    var selectMood: String? {
        didSet {
            
            self.configDatasArr()
            
            for i in 0..<moodArray.count {
                
                if let text = moodArray[i]["text"] as? String {
                    
                    if selectMood == text{
                        
                        selectIndex = i
                        break
                    }
                }
                
            }

            if let imgName:String = moodArray[selectIndex ?? 0]["imgName"] as? String{
                
                imgV.image = UIImage(named: imgName)
                    
            }
        
            DispatchQueue.main.async {
                self.collectionView.scrollToItem(at: IndexPath(item: self.selectIndex ?? 0, section: 0), at: .left, animated: false)
            }
            
        }
    }
    
    
    var selectIndex: Int?
    weak var delegate: DiaryEditMoodTableViewCellDeleagte?
    
//    let moodArray = ["开心", "惊喜", "得意", "甜蜜", "暖心", "充实", "平静", "无聊", "尴尬", "不安", "伤心", "生气", "流泪", "分裂", "疲惫", "孤独", "梦境", "不知道"]
    var moodArray:[[String:Any]] = AppConstant.feelingDatasArr
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.configDatasArr()
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "AddWeatherCollectionViewCell", bundle: .main), forCellWithReuseIdentifier: "AddWeatherCollectionViewCell")
    }
    
    func configDatasArr(){
        
        if var imgArray = UserDefaults.standard.array(forKey: KUSERFeelingImagesArrName ) as? [[String:Any]] {
            
            if let imgDeletedArray = UserDefaults.standard.array(forKey: KUSERFeelingDeletedImagesArrName) as? [[String:Any]] {
                
                imgArray.append(contentsOf: imgDeletedArray)
            }
            
            moodArray = imgArray
        }
        
        if let imgName:String = moodArray[selectIndex ?? 0]["imgName"] as? String{
            
            imgV.image = UIImage(named: imgName)
                
        }

    }

}


extension DiaryEditMoodTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let marginX = 20.0
        let itemLength = (Double(collectionView.width) - marginX * 4) / 3.2
        
        return CGSize(width: itemLength, height: 45.0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { UIEdgeInsets(top: 0, left: 20.0, bottom: 0, right: 20.0) }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 20.0 }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 20.0 }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { moodArray.count }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: AddWeatherCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddWeatherCollectionViewCell", for: indexPath) as! AddWeatherCollectionViewCell
        
        if let text = moodArray[indexPath.row]["text"] as? String {
            
            cell.label.text = text
        }
//        cell.label.text = moodArray[indexPath.row]
        
        cell.showLabel = true
        
        if selectIndex == indexPath.row {
            cell.isSelectCell = true
        } else {
            cell.isSelectCell = false
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        for i in 0..<collectionView.numberOfItems(inSection: 0) {
            if i == indexPath.row {
                selectIndex = i
                break
            }
        }
        
        collectionView.reloadData()
        
        if let imgName:String = moodArray[indexPath.row]["imgName"] as? String{
            
            imgV.image = UIImage(named: imgName)
                
        }
        
        if let text = moodArray[indexPath.row]["text"] as? String {
            
            delegate?.diaryEditMood(mood: text)
        }
        
        
        
    }
    
}
